using System; 
using Com.Mathphoenix.MapApps;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Com.Mathphoenix.MapApps.NerdDinner.Models;
using Com.Mathphoenix.MapApps.NerdDinner.Helpers;
namespace  Com.Mathphoenix.MapApps.NerdDinner.Controllers
{
    public class DinnersController : Controller
    {

        DinnerRepository dinnerRepository = new DinnerRepository();

        //
        // GET: /Dinners/

        public ActionResult Index(int? page)
        {
            //var dinners = dinnerRepository.FindUpcomingDinners().ToList();

            //return View("Index", dinners);
            const int pageSize = 10;

            var upcomingDinners = dinnerRepository.FindUpcomingDinners();

            //var paginatedDinners = upcomingDinners.Skip((page ?? 0) * pageSize)
            //    .Take(pageSize)
            //    .ToList();

            var paginatedDinners = new PaginatedList<Dinner>(upcomingDinners, page ?? 0, pageSize);

            return View(paginatedDinners);
        }


        //
        // HTTP-GET: /Dinners/Details/2

        public ActionResult Details(int id)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);

            if (dinner == null)
                return View("NotFound");
            else
                return View(dinner);
            //Response.Write("<h1> Details dinnerID:" + id + "</h1>");

        }



        //
        // Get: /Dinners/Edit/2
        [Authorize]
        public ActionResult Edit(int id)
        {
            //Dinners dinner = dinnerRepository.GetDinner(id);

            //return View(dinner);

            Dinner dinner = dinnerRepository.GetDinner(id);

            if (!dinner.IsHostedBy(User.Identity.Name)) return View("InvalidOwener");

            return View(new DinnerFormViewModel(dinner));
        }


        //
        // POST: /Dinners/Edit/2

        [AcceptVerbs(HttpVerbs.Post),Authorize]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            ////Dinners dinner = dinnerRepository.GetDinner(id);

            ////UpdateModel(dinner);

            ////dinnerRepository.Save();

            ////return RedirectToAction("Details", new { id = dinner.DinnerID });

            //Dinners dinner = dinnerRepository.GetDinner(id);

            //try
            //{
            //    UpdateModel(dinner);

            //    dinnerRepository.Save();

            //    return RedirectToAction("Details", new { id = dinner.DinnerID });
            //}
            //catch
            //{
            //    //foreach (var issue in dinner.GetRuleViolations())
            //    //{
            //    //    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
            //    //}
            //    ModelState.AddRuleViolations(dinner.GetRuleViolations());

            //    return View(dinner);
            //}

            Dinner dinner = dinnerRepository.GetDinner(id);

            if (!dinner.IsHostedBy(User.Identity.Name)) return View("InvalidOwner");


            try
            {
                UpdateModel(dinner);
                dinnerRepository.Save();

                return RedirectToAction("Details", new { id = dinner.DinnerID });
            }
            catch
            {
                //ModelState.AddModelError((dinner.GetRuleViolations());
                ModelState.AddRuleViolations(dinner.GetRuleViolations());
                return View(new DinnerFormViewModel(dinner));
            }
        }

        //
        // GET: /Dinners/Create
        [Authorize]
        public ActionResult Create()
        {
            Dinner dinner = new Dinner()
            {
                EventDate = DateTime.Now.AddDays(7)
            };
            return View(new DinnerFormViewModel(dinner));
        }


        //
        // Fix :/Dinners/Fix
        //public ActionResult Fix()
        //{
        //    NerdDinnerDataContext db = new NerdDinnerDataContext();
        //    var dinner = from aDinner in db.Dinners
        //                 select aDinner;
        //    foreach (var d in dinner)
        //    {
        //        d.HostedBy = "cong.zhang";
        //    }

        //    db.SubmitChanges();

        //    var upcomingdinners = dinnerRepository.FindUpcomingDinners();

        //    var paginateDinners = new PaginatedList<Dinner>(upcomingdinners, 0, 100);
        //    return View("index", paginateDinners);            
        //}


        //
        //
        // POST: /Dinners/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Create(Dinner dinner)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    dinner.HostedBy = User.Identity.Name;

                    dinnerRepository.Add(dinner);
                    dinnerRepository.Save();
                    return RedirectToAction("Details", new  { id = dinner.DinnerID });
                }
                catch 
                {
                    ModelState.AddRuleViolations(dinner.GetRuleViolations());
                }
            }

            return View(new DinnerFormViewModel(dinner));

        }


        //
        // HTTP GET: /Dinners/Delete/1

        public ActionResult Delete(int id)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);

            if (dinner == null)
                return View("NotFound");
            else
                return View(dinner);
        }




        //
        // HTTP POST: /Dinners/Delete/1

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, string confirmButton)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);

            if (dinner == null)
                return View("NotFound");
            dinnerRepository.Delete(dinner);
            dinnerRepository.Save();

            return View("Deleted");
        }
    }


    public class DinnerFormViewModel
    {
        // Properties
        public Dinner Dinner { get; private set; }
        public SelectList Countries {get; private set;}


        // Constructor
        public DinnerFormViewModel(Dinner dinner){
            Dinner=dinner;
            Countries = new SelectList(PhoneValidator.Countries, dinner.Country);
        }
    }






}
